
using Redis_demo.utils;
using StackExchange.Redis;

namespace Redis_demo.Middlewares;

public class RefreshTokenMiddleware : IMiddleware
{
    public readonly IConnectionMultiplexer _connectionMultiplexer;

    public RefreshTokenMiddleware(IConnectionMultiplexer connectionMultiplexer)
    {
        _connectionMultiplexer = connectionMultiplexer;
    }

    public Task InvokeAsync(HttpContext context, RequestDelegate next)
    {
        // 1从header中获取token
        var token = context.Request.Headers.FirstOrDefault(x => x.Key == "token");
        if(token.Value != ""){
            var dataBase = _connectionMultiplexer.GetDatabase();
            var key = dataBase.KeyExists(RedisConstants.Login_User_Key+token.Value.ToString());
            if(key){
                dataBase.KeyExpire(RedisConstants.Login_User_Key+token.Value.ToString(), DateTime.Now.AddMinutes(RedisConstants.Login_User_Til));
            }
        }
       return next(context);

        // 2在redis中判断token是否存在

        // 3不存在则放行

        // 3.2存在，则刷新token的时间

    }
}
